<template>
  <div>
    <div class="pagination">
      <button
        :disabled="pageNo == 1"
        @click="$emit('currentChange', pageNo - 1)"
      >
        上一页
      </button>
      <button
        @click="$emit('currentChange', 1)"
        v-if="startAndEndNumber.start > 1"
      >
        1
      </button>

      <button v-if="startAndEndNumber.start > 2">···</button>

      <button
        :class="{ active: pageNo == page }"
        @click="$emit('currentChange', page)"
        v-for="(page, index) in startAndEndNumber.end"
        :key="index"
        v-if="page >= startAndEndNumber.start"
      >
        {{ page }}
      </button>

      <button v-if="startAndEndNumber.end < totalPage - 1">···</button>
      <button
        @click="$emit('currentChange', totalPage)"
        v-if="startAndEndNumber.end < totalPage"
      >
        {{ totalPage }}
      </button>
      <button
        @click="$emit('currentChange', pageNo + 1)"
        :disabled="pageNo == totalPage"
      >
        下一页
      </button>
      <select class="select" @change="handler" v-model="ps">
        <option v-for="(item, index) in pageSizes" :key="index" :value="item">
          {{ item }}条/页
        </option>
      </select>
      <button style="margin-left: 30px">共 {{ total }} 条</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  data() {
    return {
      //收集下拉菜单用户选中的数据
      ps: "10",
    };
  },
  //接受使用这个组件父亲传递过来的数据
  props: {
    //当前页码
    pageNo: {
      type: Number,
      default: 1,
    },
    //总条数
    total: {
      type: Number,
      //当前字段务必传递
      require: true,
    },
    //一页多少条数据
    pageSize: {
      type: Number,
      default: 10,
    },
    //连续页码数
    pager: {
      type: Number,
      default: 5,
    },
    //下拉菜单需要的数据
    pageSizes: {
      type: Array,
      //props:default默认数值 如果引用类型数据 箭头函数形式
      default: () => {
        return [10, 20, 30, 40];
      },
    },
  },
  //计算属性
  computed: {
    //总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    //计算出分页器连续页码数(中间),开头数字与结尾数字
    startAndEndNumber() {
      //存储开始与结束的数字变量
      let start, end;
      const { totalPage, pager, pageNo } = this;
      //非法情况的判断--->万一出现总个数小于连续页码数
      if (totalPage < pager) {
        start = 1;
        end = totalPage;
      } else {
        //正常情况:总页码大于等于连续页码(至少五页)
        start = pageNo - parseInt(pager / 2);
        end = pageNo + parseInt(pager / 2);
        //左侧约束范围:出现非法的情况纠正
        if (start < 1) {
          start = 1;
          end = pager;
        }
        //右侧约束范围:出现非法的情况纠正
        if (end > totalPage) {
          end = totalPage;
          start = totalPage - pager + 1;
        }
      }

      return { start, end };
    },
  },
  methods: {
    //下拉菜单的change事件回调
    handler() {
      //触发自定义事件:给父组件传递过来
      this.$emit("sizeChange", this.ps);
    },
  },
};
</script>
<style lang="less" scoped>
.pagination {
  display: flex;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
  .select {
    width: 70px;
  }
}
</style>